package yi.commons.exceptions;

/**
 * 异常投射器，用于方便的抛出异常
 *
 * @author wangda
 * @date 2020-10-13
 */
public interface ExceptionThrower {

    public String getErrorCode();

    public String getErrorDesc();

    public ExceptionMessageFormater getMessageFormatter();

    public String formatMessage(Object param);

    public CommonException buildException();

    public CommonException buildException(String msg);

    public CommonException buildException(String msg, Throwable throwable);

    public CommonException buildException(Throwable throwable);

    default CommonException build(Throwable throwable, String... params) {
        String msg = getMessageFormatter().format(params);
        CommonException ex = buildException(msg, throwable);
        ex.setErrorCode(getErrorCode(), getErrorDesc());
        return ex;
    }

    default void buildAndThrow(Throwable throwable, String... params) {
        CommonException ex = build(throwable, params);
        throw ex;
    }
}
